/**
 * Created by lesonchan on 2017/9/30.
 */
import Vue from "vue";
import axios from "axios";

const host = location.href
const signLink = location.href.split('#')[0]
// axios 配置
axios.defaults.timeout = 10000;

if(process.env.NODE_ENV == 'development') {
  axios.defaults.baseURL = 'http://qsms.gzqnyf.com';
}else {
  
  // 测试号 + 正式公众号
  if(host.indexOf('qsls.gzqnyf.com') >= 0) {
    axios.defaults.baseURL = 'http://qsls.gzqnyf.com';
  }else {
    axios.defaults.baseURL = 'http://qsms.gzqnyf.com';
  }
}

// http request 拦截器
axios.interceptors.request.use(
  config => {
    
    const OPENID = localStorage.openId
    if (OPENID) {
      let tokens = btoa(OPENID + ':')
      config.headers.Authorization = `Basic ${tokens}`;
    }
    return config;
  },
  err => {
    return Promise.reject(err);
  });
  
// http response 拦截器
axios.interceptors.response.use(
  response => {
    return response;
  },
  error => {
    if (error.response.status === 401) {
      switch (error.response.request.responseText) {
        case '0':
          break
        case '1':
          break
        case '2':
          break
        case '3':
            if(signLink.indexOf('system.html')>0) {
              window.location = 'system.html#/orderlogin'
            }else {
              window.location = 'index.html#/login'
            }
            
          break
        case '4':
            window.location = 'index.html#/reg?active=1'
          break
        case '5':
            window.location = 'index.html#/reg?active=2'
          break
      }
    } else {
      //alert('WEB:' + error.response.data.message)
    }
    // console.log(JSON.stringify(error));//console : Error: Request failed with status code 402
    return Promise.reject(error.response.data)
  });

export default axios;
